//
//  ForViewController.swift
//  UIDemo
//
//  Created by MAC on 2016/12/30.
//  Copyright © 2016年 MAC. All rights reserved.
//

import UIKit

class ForViewController: UIViewController {

    var imgs = ["angelababy.jpg","陈乔恩.jpg","陈妍希.jpg","陈好.jpg","柳岩.jpg","张馨予.jpg","陈紫涵.jpg","郭碧婷.png"]
    var titles = ["angelababy","陈乔恩","陈妍希","陈好","柳岩","张馨予","陈紫函","郭碧婷"]
    var colors = [UIColor.cyan,UIColor.red,UIColor.orange,UIColor.blue,UIColor.brown,UIColor.green,UIColor.purple,UIColor.magenta]
    
    var bgView = UIView()
    
    lazy var translucentView = UIView()
    lazy var quxiaoBtn = UIButton()
    lazy var images = UIImageView()
    var st = NSString()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        
        //接收上级界面传过来的值，并打印
        print("======",st)
        
        bgView = UIView(frame:CGRect(origin:CGPoint(x:0,y:64),size:CGSize(width:self.view.frame.size.width,height:self.view.frame.height-64)))
        bgView.backgroundColor = UIColor.gray
        self.view.addSubview(bgView);
        
        let hWidth = Int((self.view.frame.size.width/3)-13)
        
        print("",hWidth)
        //for循环按钮
        for index in 0..<titles.count {
            
            let btn = UIButton(type:.custom)
           
	    btn.frame = CGRect(origin:CGPoint(x: 10+(index%3)*(hWidth+10),y:10+(index/3)*(hWidth+10)),size:CGSize(width:hWidth,height:hWidth))
            btn.tag = 100+index
            btn.setImage(UIImage(named:imgs[index]), for: UIControlState.normal)
            btn.backgroundColor = colors[index]
            btn.addTarget(self, action: #selector(btnClick(sender:)), for: UIControlEvents.touchUpInside)
            bgView.addSubview(btn)
            print("",index)
        }
        
        // Do any additional setup after loading the view.
    }
    
    @objc func btnClick(sender:UIButton)  {
        print("-----",sender.tag);
        
        translucentView.frame = bgView.bounds
        translucentView.backgroundColor = UIColor(red:  0.11, green: 0.11, blue: 0.11, alpha: 0.5)

//        translucentView.alpha = 0.5
        bgView.addSubview(translucentView)
        
        /****************** 算出比例 ********************/
        //先获取图片原始宽高，然后在用原始的宽除以原始的高，得出比例
        let bili = (sender.imageView?.image?.size.width)!/(sender.imageView?.image?.size.height)!
        
        //用固定的宽度去除以比例，得出图片的高度
        let gaibianHeight = 280/bili
        
        //打印高度和比例
        print("",bili,gaibianHeight)
        /****************** 算出比例 end********************/
        
        
        images.frame=CGRect(origin:CGPoint(x:20,y:40),size:CGSize(width:280,height: gaibianHeight))
        images.backgroundColor = UIColor.gray
        
        images.image = sender.imageView?.image
        
        bgView.addSubview(images)
        
        //移除视图按钮
        quxiaoBtn.frame = CGRect(origin:CGPoint(x:285,y:10),size:(CGSize(width:30,height:30)))
//        quxiaoBtn.backgroundColor = UIColor.orange
        quxiaoBtn.setTitle("×", for: UIControlState.normal)
        quxiaoBtn.setTitleColor(UIColor.lightGray, for: UIControlState.normal)
        quxiaoBtn.titleLabel?.font = UIFont.systemFont(ofSize: 35)
        quxiaoBtn.layer.cornerRadius = 15
        quxiaoBtn.addTarget(self, action: #selector(removeClick), for: UIControlEvents.touchUpInside)
        bgView.addSubview(quxiaoBtn)
        
    }
    
    //view的系统点击事件
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        print("-- Began")
        removeClick()//移除视图
    }
    
    //移除视图
    @objc func removeClick() {
        translucentView.removeFromSuperview()//移除视图
        images.removeFromSuperview()//移除视图
        quxiaoBtn.removeFromSuperview()//移除视图
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
